package exception;

import java.io.IOException;

/**
 * 测试异常的抛出
 */
public class Student {
    private int age;

    public Student() {
    }
    public Student(int age) {
        this.age = age;
    }

    public int getAge() {
        return age;
    }
    public void setAge(int age) throws IllegalAgeException {//抛给调用方
        if(age<0 || age>100){
            /*
            当使用throw抛出一个人检查异常时，必须在当前方法上用throws声明该异常的抛出
             */
            throw new IllegalAgeException("年龄不合法");//抛出检查异常，是必须被处理的
        }
        this.age = age;
    }



    /*
    private int age;

    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        if (age<0 || age>100){
            throw new RuntimeException();//抛出了一个异常
        }
        this.age = age;
    }

     */
}

